<template>
    <rx-dialog @handOk="handTrans" @cancel="cancel" order="bottom">
        <rx-layout>
            <div slot="center">
                <a-form-model ref="form" :model="form" :rules="rules">
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.taskName','任务名称')">
                                {{ form.subject }}
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.transferType','流转类型')"
                                               prop="transferType">
                                <a-radio-group :options="transferOptions" v-model="form.transferType">
                                </a-radio-group>
                            </a-form-model-item>
                        </a-col>
                    </a-row>

                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.transferor','流转人')"
                                               prop="toUser">
                                <!--rx-input-button width="100%" v-model="form.toUser"
                                                 @click="selectUsers"></rx-input-button-->
                                <rx-user-component v-model="form.toUser" :isAccount="false" :single="false"></rx-user-component>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.approveType','审批类型')"
                                               prop="approveType">
                                <a-radio-group :options="approveOptions" v-model="form.approveType">
                                </a-radio-group>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row>
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.remarks','备注')"
                                               prop="opinion">
                                <a-textarea v-model="form.opinion" style="width: 100%"/>
                            </a-form-model-item>
                        </a-col>
                    </a-row>


                    <a-row v-show="form.approveType=='parallel'">
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.completeType','完成配置')"
                                               prop="completeType">
                                <a-radio-group :options="completeOptions" v-model="form.completeType">
                                </a-radio-group>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row v-show="form.completeType==1 && form.approveType=='parallel'">
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.judgeType','计算类型')"
                                               prop="completeJudgeType">
                                <a-radio-group :options="completeJudgeOptions" v-model="form.completeJudgeType">
                                </a-radio-group>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                    <a-row v-show="form.completeType==1 && form.approveType=='parallel'">
                        <a-col :span="24">
                            <a-form-model-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('flowPage.votes','投票数')"
                                               prop="completeSetting">
                    <span style="width:80px;display: inline-block;margin-right: 6px;">
                        <a-input-number v-model="form.completeSetting" :min="1" :max="form.completeJudgeType=='voteCount'?form.toUser.length:100" style="width:100px;"/>
                    </span>
                                <span v-if="form.completeJudgeType=='voteCount'">{{i18n('flowPage.ticket','票')}}</span>
                                <span v-else>%</span>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </div>
        </rx-layout>

    </rx-dialog>
</template>
<script>

import BpmPublicApi from '@/api/bpm/core/BpmPublicApi'
import {BaseFormModel, RxDialog, Dialog, Util, RxUserComponent} from '@lowcode/jpaas-common-lib';
import BpmtaskApi from "@/api/bpm/core/bpmTask";

export default {
    name: 'BpmTaskRoamTransfer',
    props: {
        subject: String,
        taskId: String,
        layerid: String,
        lydata: Object,
        destroy: Function
    },
    mixins: [BaseFormModel],
    components: {
        RxDialog,
        RxUserComponent
    },
    data() {
        return {
            rules: {
                toUser: [{required: true, message: this.i18n('flowPage.transferor','请选择流转人','select'), trigger: 'change'}]
            },
            msgOptions: [],
            transferOptions: [
                {label: this.i18n('flowPage.options.return_','返回'), value: 'waitAllVoted'},
                {label: this.i18n('flowPage.options.next','审批下一步'), value: 'jumpNext'}
            ],
            approveOptions: [
                {label: this.i18n('flowPage.options.serial','串行'), value: 'sequential'},
                {label: this.i18n('flowPage.options.parallel','并行'), value: 'parallel'}
            ],
            completeOptions: [
                {label: this.i18n('flowPage.options.default','默认'), value: 0},
                {label: this.i18n('flowPage.options.advancedConf','高级配置'), value: 1}
            ],
            completeJudgeOptions: [
                {label: this.i18n('flowPage.options.voteNumber','票数'), value: 'voteCount'},
                {label: this.i18n('flowPage.options.percentage','百分比'), value: 'votePercent'}
            ],
            form: {
                transferType: "waitAllVoted",
                approveType: "parallel",
                completeType: 0,
                completeJudgeType: "voteCount",
                completeSetting: 1,
                opinion: "",
                toUser: [],
                msgTypes: []
            },
            aryNames: [],
            aryIds: []
        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"bpmTask",key);
        },
        init() {
            this.form.subject = this.subject;
            this.form.taskId = this.taskId;

            BpmPublicApi.getMessageHandler().then(res => {
                res.forEach((val) => {
                    this.msgOptions.push({label: val.name, value: val.type})
                })
            })
        },

        handTrans(e) {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    e.loading = false;
                    return;
                }
                var self_ = this;
                this.form.msgType = this.form.msgTypes.join(",");
                this.$confirm({
                    title: self_.i18n('flowPage.confirmFlowTips','确认流转任务吗?'),
                    cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                    okText: this.$ti18('rxForm.ackBtn','确认'),
                    zIndex:9999,
                    onOk() {
                        BpmtaskApi.transRoamTask(self_.form).then(res => {
                            e.loading = false;
                            if (res.success) {
                                Util.closeWindow(self_, "ok")
                            }
                        })
                    },
                    onCancel() {
                        e.loading = false;
                        return;
                    },
                });

            });
        }
    }
}
</script>
